<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="立项年度" prop="year">
        <el-select v-model="queryParams.year" placeholder="请选择立项年度" clearable>
          <el-option
            v-for="dict in dict.type.year"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同名称" prop="htName">
        <el-input
          v-model="queryParams.htName"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同状态:" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择合同状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.process_business_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同编号" prop="htBh">
        <el-input
          v-model="queryParams.htBh"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['htbz:htbz:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['htbz:htbz:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['htbz:htbz:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['htbz:htbz:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['htbz:htbz:import']"
        >导入
        </el-button>
      </el-col>
      <el-button
        size="mini"
        type="primary"
        icon="el-icon-delete"
        @click="handleExports"
      >导出docx</el-button>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="!selectedRow || (selectedRow.processStatus !== 1 && selectedRow.processStatus !== 4)"
          @click="handleStartTask"
        >发起申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-delete"
          @click="handleRevokeProcess"
          :disabled="!selectedRow || selectedRow.processStatus !== 2"
        >撤销流程</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              v-loading="loading" :data="htbzList" @selection-change="handleSelectionChange"
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="年度" align="center" prop="year" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.year" :value="scope.row.year"/>
        </template>
      </el-table-column>
      <el-table-column label="合同编号" align="center" prop="htBh" show-overflow-tooltip/>
      <el-table-column label="合同名称" align="center" prop="htName" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.htName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="课题归属" align="center" prop="ktGs" width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kylx_ktgs" :value="scope.row.ktGs"/>
        </template>
      </el-table-column>
      <el-table-column label="承担单位" align="center" prop="cdDw" show-overflow-tooltip/>
      <el-table-column label="负责人" align="center" prop="fzName"/>
      <el-table-column label="项目总经费(万元)" align="center" prop="xmZjf" width="130"/>
      <el-table-column label="甲方支付(万元)" align="center" prop="jfZf"/>
<!--      <el-table-column label="关联项目" align="center" prop="glXm"/>-->
<!--      <el-table-column label="项目申报ID" align="center" prop="xmsbId"/>-->
      <el-table-column label="起时间" align="center" prop="qTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="止时间" align="center" prop="zTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="processStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.process_business_status" :value="scope.row.processStatus" />
        </template>
      </el-table-column>
      <el-table-column label="录入人员" align="center" prop="lrRy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.processStatus==1 || scope.row.processStatus==4"
            v-hasPermi="['htbz:htbz:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.processStatus==1 || scope.row.processStatus==4"
            v-hasPermi="['htbz:htbz:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入对话框 -->
    <HtDr ref="htDr"></HtDr>

    <!-- 添加或修改合同编制对话框 -->
    <vxe-modal :title="title" v-model="open" width="80%" showFooter show-zoom resize >
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="158px">

        <el-tabs tab-position="right" type="border-card" v-model="activeName">
          <el-tab-pane name="htJbzl" label="1.合同基本资料">
            <div v-if="activeName === 'htJbzl'" style="height: 75vh; overflow-y: auto;">

              <!-- 合同基本信息 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">合同基本信息</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="合同名称" prop="htName">
                      <el-input v-model="form.htName" placeholder="请输入合同名称"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="关联项目" prop="glXm">
                      <el-input disabled v-model="form.glXm" placeholder="请输入关联项目" @focus="selectKt">
                        <el-button slot="append" icon="el-icon-search" @click="selectKt" style="color: #1890ff">查询课题列表
                        </el-button>
                      </el-input>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-form-item label="项目申报ID" prop="xmsbId">
                  <el-input v-model="form.xmsbId" placeholder="请输入项目申报ID"/>
                </el-form-item>
                <el-form-item label="年度" prop="year">
                  <el-input v-model="form.year" placeholder="请输入年度"/>
                </el-form-item>
                <el-form-item label="课题归属" prop="ktGs">
                  <el-select v-model="form.ktGs" placeholder="请选择课题归属">
                    <el-option
                      v-for="dict in dict.type.kylx_ktgs"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="合同编号" prop="htBh">
                      <el-input v-model="form.htBh" placeholder="请输入合同编号"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="密级" prop="mj">
                      <el-select v-model="form.mj" placeholder="请选择密级">
                        <el-option
                          v-for="dict in dict.type.htbz_mj"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="起止年月" prop="qTime">
                      <!--                      <el-date-picker clearable-->
                      <!--                                      v-model="form.qTime"-->
                      <!--                                      type="month"-->
                      <!--                                      value-format="yyyy-MM"-->
                      <!--                                      placeholder="请选择起时间">-->
                      <!--                      </el-date-picker>-->
                      <!--                      至-->
                      <!--                      <el-date-picker clearable-->
                      <!--                                      v-model="form.zTime"-->
                      <!--                                      type="month"-->
                      <!--                                      value-format="yyyy-MM"-->
                      <!--                                      placeholder="请选择止时间">-->
                      <!--                      </el-date-picker>-->

                      <el-date-picker
                        v-model="form.qTime"
                        type="monthrange"
                        range-separator="至"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <!--                <el-form-item label="止时间" prop="zTime">-->
                    <!--                  <el-date-picker clearable-->
                    <!--                                  v-model="form.zTime"-->
                    <!--                                  type="date"-->
                    <!--                                  value-format="yyyy-MM-dd"-->
                    <!--                                  placeholder="请选择止时间">-->
                    <!--                  </el-date-picker>-->
                    <!--                </el-form-item>-->
                    <el-form-item label="录入人员" prop="lrRy">
                      <el-input v-model="form.lrRy" placeholder="请输入录入人员"/>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>

              <!-- 项目承担单位 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">项目承担单位</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="承担单位" prop="cdDw">
                      <el-input v-model="form.cdDw" placeholder="请输入承担单位"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="乙方法人/授权代表" prop="yfFr">
                      <el-input v-model="form.yfFr" placeholder="请输入乙方法人/授权代表"/>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="开户银行" prop="khYh">
                      <el-input v-model="form.khYh" placeholder="请输入开户银行"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="银行账号" prop="yhZh">
                      <el-input v-model="form.yhZh" placeholder="请输入银行账号"/>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="开户名称" prop="khName">
                      <el-input v-model="form.khName" placeholder="请输入开户名称"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="签订日期" prop="qdTime">
                      <el-date-picker clearable
                                      v-model="form.qdTime"
                                      type="date"
                                      value-format="yyyy-MM-dd"
                                      placeholder="请选择签订日期">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>

              <!-- 负责人情况 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">负责人情况</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="负责人姓名" prop="fzName">
                      <el-input v-model="form.fzName" placeholder="请输入负责人姓名"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="负责人地址" prop="fzDz">
                      <el-input v-model="form.fzDz" placeholder="请输入负责人地址"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="负责人邮编" prop="fzYb">
                      <el-input v-model="form.fzYb" placeholder="请输入负责人邮编"/>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="负责人电话" prop="fzPhone">
                      <el-input v-model="form.fzPhone" placeholder="请输入负责人电话"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="负责人邮箱" prop="fzYx">
                      <el-input v-model="form.fzYx" placeholder="请输入负责人邮箱"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="负责人传真" prop="fzCz">
                      <el-input v-model="form.fzCz" placeholder="请输入负责人传真"/>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>

              <!-- 项目经费预算 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">项目经费预算</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="项目总经费(万元)" prop="xmZjf">
                      <el-input v-model="form.xmZjf" placeholder="请输入项目总经费(万元)"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="甲方支付(万元)" prop="jfZf">
                      <el-input v-model="form.jfZf" placeholder="请输入甲方支付(万元)"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="乙方自筹(万元)" prop="yfZc">
                      <el-input v-model="form.yfZc" placeholder="请输入乙方自筹(万元)"/>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>

              <!-- 项目经费支付计划 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">项目经费支付计划</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="合同总额(万元)" prop="htZe">
                      <el-input v-model="form.htZe" placeholder="请输入合同总额(万元)"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="甲向乙提供经费(万元)" prop="jfToYf">
                      <el-input v-model="form.jfToYf" placeholder="请输入甲方向乙方提供经费(万元)"/>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="zyyjnr" label="2.主要研究内容">
            <div style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">1.课题主要技术内容</span>
                </div>
                <editor v-model="form.zyYjNr" :height="192"/>
              </el-card>
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">2.主要技术难点</span>
                </div>
                <editor v-model="form.zyJsNd" :height="192"/>
              </el-card>
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">3.研究路线及研究方法</span>
                </div>
                <editor v-model="form.yjlxYjff" :height="192"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="zyjsjjzb" label="3.主要技术经济指标">
            <div v-if="activeName === 'zyjsjjzb'" style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">主要技术经济指标</span>
                </div>
                <editor v-model="form.jsJjZb" height="550"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="tgdzzcg" label="4.提供的最终成果">
            <div style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">最终成果内容及形式</span>
                </div>
                <editor v-model="form.cgnrAndXs" :height="192"/>
              </el-card>
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">成果验收方式</span>
                </div>
                <editor v-model="form.cgYsFf" :height="192"/>
              </el-card>
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">成果验收标准</span>
                </div>
                <editor v-model="form.cgYsBz" :height="192"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="ndjhkhmb" label="5.年度计划和考核目标">
            <div style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">年度计划和考核目标</span>
                </div>
                <el-row :gutter="10" class="mb8">
                  <el-col :span="1.5">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddHtglHtbzNdjh">添加
                    </el-button>
                  </el-col>
                  <el-col :span="1.5">
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHtglHtbzNdjh">删除
                    </el-button>
                  </el-col>
                </el-row>
                <el-table :data="htglHtbzNdjhList" :row-class-name="rowHtglHtbzNdjhIndex"
                          :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                          @selection-change="handleHtglHtbzNdjhSelectionChange" ref="htglHtbzNdjh">
                  <el-table-column type="selection" width="50" align="center"/>
                  <el-table-column label="序号" align="center" prop="index" width="50"/>
                  <el-table-column label="开始年月" prop="ksRq" align="center">
                    <template slot-scope="scope">
                      <el-date-picker clearable v-model="scope.row.ksRq" type="month" value-format="yyyy-MM"
                                      placeholder="请选择开始日期" style="width: 95%;"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="结束年月" prop="jsRq" align="center">
                    <template slot-scope="scope">
                      <el-date-picker clearable v-model="scope.row.jsRq" type="month" value-format="yyyy-MM"
                                      placeholder="请选择结束日期" style="width: 95%;"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="研发内容" align="center" prop="yfNr">
                    <template slot-scope="scope">
                      <el-input :rows="1" v-model="scope.row.yfNr" type="textarea" placeholder="请输入研发内容"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="阶段目标" align="center" prop="jdMb">
                    <template slot-scope="scope">
                      <el-input :rows="1" v-model="scope.row.jdMb" type="textarea" placeholder="请输入阶段目标"/>
                    </template>
                  </el-table-column>
                </el-table>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="xmdjfys" label="6.项目的经费预算">
            <div style="height: 75vh; overflow-y: auto;">
              <!-- 课题经费 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">课题经费</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="项目总经费(万元)" prop="xmZjf">
                      <el-input disabled v-model="form.xmZjf" placeholder="请输入项目总经费(万元)"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="甲方支付(万元)" prop="jfZf">
                      <el-input disabled v-model="form.jfZf" placeholder="请输入甲方支付(万元)"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="乙方自筹(万元)" prop="yfZc">
                      <el-input disabled v-model="form.yfZc" placeholder="请输入乙方自筹(万元)"/>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
              <!-- 项目预算经费 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">项目预算经费</span>
                </div>
                <el-table height="52vh" :data="htglHtbzYsjfList"
                          :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                          :row-class-name="rowHtglHtbzYsjfIndex" ref="htglHtbzYsjf">
                  <!--                  <el-table-column type="selection" width="50" align="center" />-->
                  <el-table-column label="序号" align="center" prop="index" width="50"/>
                  <el-table-column label="项目" show-overflow-tooltip header-align="center" width="400" prop="xm"/>
                  <el-table-column align="center" label="项目总预算(万元)" prop="xmZys"/>
                  <el-table-column align="center" label="第一年度预算" prop="oneYs"/>
                  <el-table-column align="center" label="第二年度预算" prop="towYs"/>
                  <el-table-column align="center" label="第三年度预算" prop="threeYs"/>
                  <el-table-column align="center" label="第四年度预算" prop="fourYs"/>
                  <el-table-column align="center" label="第五年度预算" prop="fiveYs"/>
                </el-table>
              </el-card>

            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="xmjfzfjh" label="7.项目经费支付计划">
            <div style="height: 75vh; overflow-y: auto;">
              <!-- 课题经费 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">课题经费</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="合同总额(万元)" prop="htZe">
                      <el-input disabled v-model="form.htZe" placeholder="请输入合同总额(万元)"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="甲向乙提供经费(万元)" prop="jfToYf">
                      <el-input disabled v-model="form.jfToYf" placeholder="请输入甲方向乙方提供经费(万元)"/>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
              <br>
              <!-- 项目经费支付计划 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">项目经费支付计划</span>
                </div>
                <el-row :gutter="10" class="mb8">
                  <el-col :span="1.5">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddHtglHtbzZfjh">添加
                    </el-button>
                  </el-col>
                  <el-col :span="1.5">
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHtglHtbzZfjh">删除
                    </el-button>
                  </el-col>
                </el-row>
                <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                          :data="htglHtbzZfjhList" :row-class-name="rowHtglHtbzZfjhIndex" height="45vh"
                          @selection-change="handleHtglHtbzZfjhSelectionChange" ref="htglHtbzZfjh">
                  <el-table-column type="selection" width="50" align="center"/>
                  <el-table-column label="序号" align="center" prop="index" width="50"/>
                  <el-table-column label="年度" align="center" prop="nd" width="260">
                    <template slot-scope="scope">
                      <el-date-picker clearable v-model="scope.row.nd" type="year" value-format="yyyy"
                                      placeholder="请选择年度"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="收款单位" align="center" prop="skDw">
                    <template slot-scope="scope">
                      <el-input :rows="1" type="textarea" v-model="scope.row.skDw" placeholder="请输入收款单位"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="金额(万元)" align="center" prop="je" width="180">
                    <template slot-scope="scope">
                      <el-input type="number" v-model.number="scope.row.je" placeholder="请输入金额(万元)"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="其中贷款(万元)" align="center" prop="qzDk" width="180">
                    <template slot-scope="scope">
                      <el-input type="number" v-model.number="scope.row.qzDk" placeholder="请输入其中贷款(万元)"/>
                    </template>
                  </el-table-column>
                </el-table>
              </el-card>

            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="ktcdry" label="8.课题承担人员表">
            <div style="height: 75vh; overflow-y: auto;">
              <!-- 课题承担人员表 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">课题承担人员表</span>
                </div>
                <el-row :gutter="10" class="mb8">
                  <el-col :span="1.5">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddHtglHtbzCdry">添加
                    </el-button>
                  </el-col>
                  <el-col :span="1.5">
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHtglHtbzCdry">删除
                    </el-button>
                  </el-col>
                </el-row>
                <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                          :data="htglHtbzCdryList" :row-class-name="rowHtglHtbzCdryIndex"
                          @selection-change="handleHtglHtbzCdrySelectionChange" ref="htglHtbzCdry">
                  <el-table-column type="selection" width="50" align="center"/>
                  <el-table-column label="序号" align="center" prop="index" width="50"/>
                  <el-table-column label="姓名" align="center" prop="name" width="150">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.name" placeholder="请输入姓名"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="单位" align="center" prop="gzDw">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.gzDw" placeholder="请输入单位"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="年龄" align="center" prop="age" width="100">
                    <template slot-scope="scope">
                      <el-input type="number" v-model="scope.row.age"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="职务" align="center" prop="zhiWu">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.zhiWu" placeholder="请输入职务"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="职称" align="center" prop="zhiChen">
                    <template slot-scope="scope">
                      <el-select v-model="scope.row.zhiChen" placeholder="请选择职称">
                        <el-option
                          v-for="dict in dict.type.rcpt_zc"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </template>
                  </el-table-column>
                  <el-table-column label="专业" align="center" prop="zy">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.zy" placeholder="请输入专业"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="承担的主要工作" align="center" prop="bKtFg">
                    <template slot-scope="scope">
                      <el-select v-model="scope.row.bKtFg" placeholder="请选择承担的主要工作">
                        <el-option
                          v-for="dict in dict.type.kylx_zcry_ktfg"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </template>
                  </el-table-column>
                  <el-table-column label="投入月数" align="center" prop="trZys">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.trZys" placeholder="请输入投入月数"/>
                    </template>
                  </el-table-column>
                  <el-table-column v-if="1 === 2" label="科技人员ID" align="center" prop="kjryId" width="150">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.kjryId" placeholder="请输入科技人员ID"/>
                    </template>
                  </el-table-column>
                  <el-table-column align="center" label="操作" width="120">
                    <template slot-scope="scope">
                      <el-button type="text" icon="el-icon-arrow-up" size="mini" @click="moveUp(scope.$index)" :disabled="scope.$index === 0">上移</el-button>
                      <el-button type="text" icon="el-icon-arrow-down" size="mini" @click="moveDown(scope.$index)" :disabled="scope.$index === htglHtbzCdryList.length - 1">下移</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-card>

            </div>
          </el-tab-pane>

          <el-tab-pane v-if="this.form.idWhere != null" name="htfj" label="9.合同附件">
            <div v-if="activeName === 'htfj'" style="height: 75vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">合同附件信息</span>
                </div>
                <el-row :gutter="10" class="mb8">
                  <el-col :span="1.5">
                    <el-button size="mini" icon="el-icon-upload" type="primary" @click="htFjUpload">上传</el-button>
                  </el-col>
                  <el-col :span="1.5">
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHtglHtbzFj">删除
                    </el-button>
                  </el-col>
                </el-row>
                <el-table height="60vh" :data="htglHtbzFjList" :row-class-name="rowHtglHtbzFjIndex"
                          :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                          @selection-change="handleHtglHtbzFjSelectionChange" ref="htglHtbzFj">
                  <el-table-column type="selection" width="50" align="center"/>
                  <el-table-column label="序号" align="center" prop="index" width="50"/>
                  <el-table-column label="文件名称" :formatter="getFileNames" prop="fileName"/>
                  <el-table-column label="文件大小" prop="fileSize"/>
                  <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleDownload(scope.row)"
                      >下载
                      </el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-card>
            </div>
          </el-tab-pane>

        </el-tabs>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>

    <!-- 合同编制附件上传弹窗 -->
    <vxe-modal title="附件上传" v-model="fjUpload" width="25%" showFooter show-zoom resize @close="fjClose">
      <el-upload
        align="center"
        class="upload-demo"
        multiple
        :action="uploadFileUrl"
        :before-upload="handleBeforeUpload"
        :file-list="fileList"
        :limit="20"
        :on-error="handleUploadError"
        :on-exceed="handleExceed"
        :on-success="handleUploadSuccess"
        :headers="headers"
        ref="fileUpload"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          请上传
          <template> 大小不超过 <b style="color: #f56c6c">{{ 50 }}MB</b></template>
          <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b></template>
          的文件
        </div>
      </el-upload>

      <div slot="footer" class="dialog-footer" style="display: flex; justify-content: center;">
        <el-button v-if="allFilesUploaded === false" size="mini" type="primary" @click="fjClose">关 闭
        </el-button>
        <el-button v-if="allFilesUploaded === true" size="mini" type="primary" @click="fjClose">上传完成, 关闭
        </el-button>
      </div>

    </vxe-modal>

    <!-- 课题选择 -->
    <HtXzKt ref="ktXz"></HtXzKt>

    <!-- 查询进度   -->
    <Jindu ref="picture" />

    <!-- 合同详情 -->
    <HtXq ref="htXq"/>

  </div>
</template>

<script>
import {
  addHtbz,
  delHtbz, delHtFj,
  exportWord,
  getHtbz,
  getHtBzId,
  insertHtFjByHtBzId,
  listHtbz,
  selectHtBzFjList,
  selectHtglHtbzCdRyByHtbzId,
  selectHtglHtbzNdjhByHtbzId,
  selectHtglHtbzYsJfByHtbzId,
  selectHtglHtbzZfJhByHtbzId,
  updateHtbz
} from "@/api/xmgl/htgl/htbz/htbz";
import {getToken} from "@/utils/auth";
import HtDr from "@/views/xmgl/htgl/htbz/components/HtDr";
import HtXzKt from "@/views/components/ktXz/HtXzKt";
import {selectLlglXmsbByjhjdByXmsbId, selectLlglXmsbByyffysByXmsbId, selectLlglXmsbZcryByXmsbId} from "@/api/xmgl/lxgl/xmsb/xmsb";
import Jindu from "@/views/components/liuChengState/jindu";
import {revokeProcess, startTask} from "@/api/activiti/task";
import HtXq from "@/views/xmgl/htgl/htbz/components/HtXq";

export default {
  name: "Htbz",
  components: {HtXq, Jindu, HtXzKt, HtDr},
  dicts: ['htbz_mj', 'rcpt_zc', 'kylx_zcry_ktfg', 'year', 'kylx_ktgs', 'process_business_status'],
  provide() {
    return {
      updateXmsbId: (val) => {
        let xmJcXx = val; //基础信息
        console.log('从项目中的组件选择传递的基本项目信息:', xmJcXx)
        this.form.htName = xmJcXx.ktName
        this.form.year = xmJcXx.year
        this.form.glXm = xmJcXx.ktName
        this.form.xmsbId = xmJcXx.xmsbId
        this.form.htBh = xmJcXx.ktBh
        this.form.ktGs = xmJcXx.ktGs
        // 获取qTime和zTime
        let qTimeStr = xmJcXx.qTime
        let zTimeStr = xmJcXx.zTime
        // 将字符串解析为年份和月份
        let qTimeParts = qTimeStr.split('-');
        let zTimeParts = zTimeStr.split('-');
        // 创建日期对象数组
        let qTime = new Date(parseInt(qTimeParts[0]), parseInt(qTimeParts[1]) - 1);
        let zTime = new Date(parseInt(zTimeParts[0]), parseInt(zTimeParts[1]), 0);
        // 创建 monthrange 格式的数组
        this.form.qTime = [qTime, zTime]
        this.form.cdDw = xmJcXx.zrBm
        this.form.fzName = xmJcXx.fzName
        this.form.fzDz = xmJcXx.txdz
        this.form.fzYb = xmJcXx.fzYzbm
        this.form.fzPhone = xmJcXx.fzPhone
        this.form.xmZjf = xmJcXx.xmze
        this.form.jfZf = xmJcXx.sqzz
        this.form.yfZc = xmJcXx.zcje
        // 项目申报(项目计划进度和目标) 复制到-> 合同编制(年度计划和考核目标)
        selectLlglXmsbByjhjdByXmsbId(xmJcXx.xmsbId).then(res => {
          this.htglHtbzNdjhList = res.data.lxglXmsbJhjdList;
        });
        // 项目申报(研发费用预算) 复制到-> 合同编制(项目经费支付计划)
        selectLlglXmsbByyffysByXmsbId(xmJcXx.xmsbId).then(res => {
          this.htglHtbzYsjfList = res.data.lxglXmsbYffyysList;
        });
        // 项目申报(研究课题组成人员情况信息) 复制到-> 合同编制(课题人员承担表)
        selectLlglXmsbZcryByXmsbId(xmJcXx.xmsbId).then(res => {
          this.htglHtbzCdryList = res.data.lxglXmsbZcryList
        });

      }
    };
  },
  data() {
    return {
      selectedRow: null, // 保存当前选中的行
      // 年度计划子表选中数据
      checkedHtglHtbzNdjh: [],
      // 预算经费子表选中数据
      checkedHtglHtbzYsjf: [],
      // 支付计划子表选中数据
      checkedHtglHtbzZfjh: [],
      // 课题承担人员子表选中数据
      checkedHtglHtbzCdry: [],
      activeName: 'htJbzl', //默认打开的标签
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedHtglHtbzFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同编制表格数据
      htbzList: [],
      // 合同附件表格数据
      htglHtbzFjList: [],
      // 年度计划和考核目标表格数据
      htglHtbzNdjhList: [],
      // 项目预算经费表格数据
      htglHtbzYsjfList: [],
      // 项目经费支付计划表格数据
      htglHtbzZfjhList: [],
      // 课题承担人员表格数据
      htglHtbzCdryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        htName: null,
        year: null,
        ktGs: null,
        glXm: null,
        xmsbId: null,
        processStatus: null,
        htBh: null,
        cdDw: null,
        userId: this.$store.state.user.id,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        htName: [{required: true, message: "合同名称不能为空", trigger: "blur"}],
        // glXm: [{required: true, message: "关联项目不能为空", trigger: "blur"}],
        htBh: [{required: true, message: "合同编号不能为空", trigger: "blur"}],
        qTime: [{required: true, message: "起时间不能为空", trigger: "blur"}],
        lrRy: [{required: true, message: "录入人员不能为空", trigger: "blur"}],
        cdDw: [{required: true, message: "承担单位不能为空", trigger: "blur"}],
        yfFr: [{required: true, message: "乙方法人/授权代表不能为空", trigger: "blur"}],
        khYh: [{required: true, message: "开户银行不能为空", trigger: "blur"}],
        yhZh: [{required: true, message: "银行账号不能为空", trigger: "blur"}],
        khName: [{required: true, message: "开户名称不能为空", trigger: "blur"}],
        qdTime: [{required: true, message: "签订日期不能为空", trigger: "blur"}],
        fzName: [{required: true, message: "负责人姓名不能为空", trigger: "blur"}],
        fzDz: [{required: true, message: "负责人地址不能为空", trigger: "blur"}],
        fzYb: [{required: true, message: "负责人邮编不能为空", trigger: "blur"}],
        fzPhone: [{required: true, message: "负责人电话不能为空", trigger: "blur"}],
        fzYx: [{required: true, message: "负责人邮箱不能为空", trigger: "blur"}],
        fzCz: [{required: true, message: "负责人传真不能为空", trigger: "blur"}],
        xmZjf: [{required: true, message: "项目总经费(万元)不能为空", trigger: "blur"}],
        jfZf: [{required: true, message: "甲方支付(万元)不能为空", trigger: "blur"}],
        yfZc: [{required: true, message: "乙方自筹(万元)不能为空", trigger: "blur"}],
        htZe: [{required: true, message: "合同总额(万元)不能为空", trigger: "blur"}],
        jfToYf: [{required: true, message: "甲方向乙方提供经费(万元)不能为空", trigger: "blur"}],
      },
      getHtBzId: null, //从后端获取的合同编制主键ID (UUID)
      fjUpload: false, //合同附件上传弹窗
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      //要修改的 科技人员id
      htBzIdEdit: "",
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 承担人员上移方法
    moveUp(index) {
      if (index > 0) {
        const item = this.htglHtbzCdryList.splice(index, 1)[0];
        this.htglHtbzCdryList.splice(index - 1, 0, item);
      }
    },
    // 承担人员下移方法
    moveDown(index) {
      if (index < this.htglHtbzCdryList.length - 1) {
        const item = this.htglHtbzCdryList.splice(index, 1)[0];
        this.htglHtbzCdryList.splice(index + 1, 0, item);
      }
    },
    //导出按钮操作
    handleExports() {
      const row = this.selectedRow; // 获取当前选中的行
      const queryParams = this.queryParams;
      const htbzId = row.htbzId;
      this.$confirm('是否确认导出word文档?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true
        return exportWord(queryParams, htbzId); // Passing userId to the exportWord function
      }).then(response => {
        // console.log('/profile/upload/' + response.msg)
        this.$download.resource('/profile/upload/' + response.msg);
        this.loading = false
      }).catch(() => {});
    },
    // 查询详情信息
    handleButtonClick(row) {
      this.$refs.htXq.handleOpen(row);
    },
    //发起任务
    handleStartTask() {
      const row = this.selectedRow; // 获取当前选中的行
      if (!row) {
        this.$message.error("请先选择一行数据");
        return;
      }
      this.$confirm("是否确认发起申请？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          startTask({ businessKey: row.htbzId, businessType: 4 }).then(res => {
            this.getList();
            this.$modal.msgSuccess("发起成功");
            this.selectedRow = null
          });
        })
        .catch(() => {});
    },
    //撤销流程
    handleRevokeProcess() {
      const row = this.selectedRow; // 获取当前选中的行
      if (!row) {
        this.$message.error("请先选择一行数据");
        return;
      }
      this.$confirm("是否确撤销申请？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {
            businessKey: row.htbzId,
            businessType: 4,
            processInstanceId: row.processInstanceId
          };
          revokeProcess(params).then(res => {
            this.getList();
            this.$modal.msgSuccess("撤销成功");
            this.selectedRow = null
          });
        })
        .catch(() => {});
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    // 文件下载处理
    handleDownload(row) {
      const resource = row.fileName;
      // 默认方法
      this.$download.resource(resource);
    },
    // 获取文件名原名
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    //关闭上传附件弹窗后
    fjClose() {
      this.fjUpload = false
      this.fileList = []
      this.allFilesUploaded = false
    },
    //上传文件之前的钩子
    handleBeforeUpload(file) {
      let that = this;
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
          return false;
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 / 1024 < this.fileSize;
          if (!isLt) {
            this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
            return false;
          }
        }
        this.$modal.loading("正在上传文件，请稍候...");
        this.number++;
        return true;
      }
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        strs += list[i].url + separator;
      }
      return strs !== '' ? strs.substr(0, strs.length - 1) : '';
    },
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传成功
    handleUploadSuccess(res, file) {
      let that = this;
      if (res.code === 200) {
        this.uploadList.push({name: res.fileName, url: res.fileName, size: that.formatFileSize(file.size)});
        let xzItemFile = {
          "htbzId": that.getHtBzId,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        let xgItemFile = {
          "htbzId": that.htBzIdEdit,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        if (this.xg === false) {
          // 点击新增按钮的上传
          insertHtFjByHtBzId(xzItemFile).then(resFil => {
            that.getHtBzFjList();
          })
        } else if (this.xg === true) {
          // 点击修改按钮的上传
          insertHtFjByHtBzId(xgItemFile).then(resFil => {
            that.getHtBzFjList();
          })
        }
        this.uploadedSuccessfully();
      } else {
        this.number--;
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.fileUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
      this.allFilesUploaded = true
      this.$modal.msgSuccess("上传成功")
      // this.fjShangChuan = false
    },
    getHtBzFjList() {
      let that = this;
      if (this.xg === false) {
        //点击新增 上传文件后的文件列表
        selectHtBzFjList(that.getHtBzId).then(resFileList => {
          that.htglHtbzFjList = resFileList.data;
        })
      } else {
        //点击修改 上传文件后的文件列表
        selectHtBzFjList(that.htBzIdEdit).then(resFileList => {
          that.htglHtbzFjList = resFileList.data;
        })
      }
    },
    // 显示文件大小为KB, MB, GB, TB
    formatFileSize(size) {
      if (size === 0) return '0 Bytes';
      const k = 1024;
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      const i = Math.floor(Math.log(size) / Math.log(k));
      return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    },
    // 获取文件名称
    getFileName(name) {
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.number > 0 && this.uploadList.length === this.number) {
        this.fileList = this.fileList.concat(this.uploadList);
        this.fileList = this.fileList.map(item => {
          // 使用replace方法去除name中的特定前缀
          item.name = item.name.split('/').pop();
          return item;
        });
        this.uploadList = [];
        this.number = 0;
        this.$emit("input", this.listToString(this.fileList));
        this.$modal.closeLoading();
      }
    },
    // 合同附件上传按钮
    htFjUpload() {
      this.fjUpload = true
    },
    /** 查询合同编制列表 */
    getList() {
      this.loading = true;
      listHtbz(this.queryParams).then(response => {
        this.htbzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        htbzId: this.getHtBzId, year: null, ktGs: null, htName: null, glXm: null, xmsbId: null, htBh: null, mj: '秘密', qTime: null,
        zTime: null, lrRy: null, cdDw: null, yfFr: null, khYh: null, yhZh: null, khName: null, qdTime: null,
        fzName: null, fzDz: null, fzYb: null, fzPhone: null, fzYx: null, fzCz: null, xmZjf: null, jfZf: null,
        yfZc: null, htZe: null, jfToYf: null, zyYjNr: null, zyJsNd: null, yjlxYjff: null, jsJjZb: null,
        cgnrAndXs: null, cgYsFf: null, cgYsBz: null, createBy: null, createTime: null, updateBy: null,
        updateTime: null, userId: null, deptId: null, idWhere: null,
      };
      this.htglHtbzFjList = [];
      this.htglHtbzNdjhList = [];
      this.htglHtbzYsjfList = [];
      this.htglHtbzZfjhList = [];
      this.htglHtbzCdryList = [];
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.htbzId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getHtBzId().then(res => { //从后端获取合同编制主键ID (UUID)
        this.getHtBzId = res.msg
        this.reset();
      })
      this.activeName = 'htJbzl'
      this.open = true;
      this.title = "添加合同信息";
      this.selectKt() // 打开选择课题弹窗
    },
    // 打开选择课题弹窗
    selectKt() {
      this.$refs.ktXz.handleOpen();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      this.xg = true
      const htbzId = row.htbzId || this.ids
      this.htBzIdEdit = row.htbzId || this.ids
      getHtbz(htbzId).then(response => {
        this.form = response.data;
        // 将qTime和zTime转回为monthrange所需的数组格式
        let qTimeStr = response.data.qTime;
        let zTimeStr = response.data.zTime;
        // 将字符串解析为年份和月份
        let qTimeParts = qTimeStr.split('-');
        let zTimeParts = zTimeStr.split('-');
        // 创建日期对象数组
        let qTime = new Date(parseInt(qTimeParts[0]), parseInt(qTimeParts[1]) - 1);
        let zTime = new Date(parseInt(zTimeParts[0]), parseInt(zTimeParts[1]), 0);
        // 创建 monthrange 格式的数组
        this.form.qTime = [qTime, zTime]

        this.htglHtbzFjList = response.data.htglHtbzFjList;
        this.activeName = 'htJbzl'
        this.open = true;
        this.title = "修改合同编制";

        selectHtglHtbzNdjhByHtbzId(htbzId).then(res => {
          this.htglHtbzNdjhList = res.data.htglHtbzNdjhList;
        });
        selectHtglHtbzYsJfByHtbzId(htbzId).then(res => {
          this.htglHtbzYsjfList = res.data.htglHtbzYsjfList;
        });
        selectHtglHtbzZfJhByHtbzId(htbzId).then(res => {
          this.htglHtbzZfjhList = res.data.htglHtbzZfjhList;
        });
        selectHtglHtbzCdRyByHtbzId(htbzId).then(res => {
          this.htglHtbzCdryList = res.data.htglHtbzCdryList;
        });

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          this.form.htglHtbzFjList = this.htglHtbzFjList;
          this.form.htglHtbzNdjhList = this.htglHtbzNdjhList;
          this.form.htglHtbzYsjfList = this.htglHtbzYsjfList;
          this.form.htglHtbzZfjhList = this.htglHtbzZfjhList;
          this.form.htglHtbzCdryList = this.htglHtbzCdryList;
          if (this.form.idWhere != null) { //新增
            // 调整日期格式
            let qTimeStart = this.form.qTime[0].getFullYear() + '-' + (this.form.qTime[0].getMonth() + 1).toString().padStart(2, '0');
            let qTimeEnd = this.form.qTime[1].getFullYear() + '-' + (this.form.qTime[1].getMonth() + 1).toString().padStart(2, '0');
            this.form.qTime = qTimeStart
            this.form.zTime = qTimeEnd

            updateHtbz(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.xg = false
            });
          } else { //修改
            // 调整日期格式
            let qTimeStart = this.form.qTime[0].getFullYear() + '-' + (this.form.qTime[0].getMonth() + 1).toString().padStart(2, '0');
            let qTimeEnd = this.form.qTime[1].getFullYear() + '-' + (this.form.qTime[1].getMonth() + 1).toString().padStart(2, '0');
            this.form.qTime = qTimeStart
            this.form.zTime = qTimeEnd

            addHtbz(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.handleUpdate(this.form) //新增完成后打开修改弹窗
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const htbzIds = row.htbzId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delHtbz(htbzIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 合同附件序号 */
    rowHtglHtbzFjIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 合同附件删除按钮操作 */
    handleDeleteHtglHtbzFj() {
      if (this.checkedHtglHtbzFj.length == 0) {
        this.$modal.msgError("请先选择要删除的合同附件数据");
      } else {
        const fjIds = this.checkedHtglHtbzFj;
        this.$modal.confirm('确定删除选中记录？').then(function() {
          return delHtFj(fjIds);
        }).then(() => {
          getHtbz(this.htBzIdEdit).then(response => {
            this.htglHtbzFjList = response.data.htglHtbzFjList;
          });
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
    /** 复选框选中数据 */
    handleHtglHtbzFjSelectionChange(selection) {
      this.checkedHtglHtbzFj = selection.map(item => item.fjId)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('htbz/htbz/export', {
        ...this.queryParams
      }, `htbz_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.htDr.handleImport();
    },
    /** 年度计划和考核目标序号 */
    rowHtglHtbzNdjhIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 年度计划和考核目标添加按钮操作 */
    handleAddHtglHtbzNdjh() {
      let obj = {};
      obj.ksRq = "";
      obj.jsRq = "";
      obj.yfNr = "";
      obj.jdMb = "";
      this.htglHtbzNdjhList.push(obj);
    },
    /** 年度计划和考核目标删除按钮操作 */
    handleDeleteHtglHtbzNdjh() {
      if (this.checkedHtglHtbzNdjh.length == 0) {
        this.$modal.msgError("请先选择要删除的年度计划和考核目标数据");
      } else {
        const htglHtbzNdjhList = this.htglHtbzNdjhList;
        const checkedHtglHtbzNdjh = this.checkedHtglHtbzNdjh;
        this.htglHtbzNdjhList = htglHtbzNdjhList.filter(function (item) {
          return checkedHtglHtbzNdjh.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleHtglHtbzNdjhSelectionChange(selection) {
      this.checkedHtglHtbzNdjh = selection.map(item => item.index)
    },
    /** 项目预算经费序号 */
    rowHtglHtbzYsjfIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 项目经费支付计划序号 */
    rowHtglHtbzZfjhIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 项目经费支付计划添加按钮操作 */
    handleAddHtglHtbzZfjh() {
      let obj = {};
      obj.nd = "";
      obj.skDw = "";
      obj.je = "";
      obj.qzDk = "";
      this.htglHtbzZfjhList.push(obj);
    },
    /** 项目经费支付计划删除按钮操作 */
    handleDeleteHtglHtbzZfjh() {
      if (this.checkedHtglHtbzZfjh.length == 0) {
        this.$modal.msgError("请先选择要删除的项目经费支付计划数据");
      } else {
        const htglHtbzZfjhList = this.htglHtbzZfjhList;
        const checkedHtglHtbzZfjh = this.checkedHtglHtbzZfjh;
        this.htglHtbzZfjhList = htglHtbzZfjhList.filter(function (item) {
          return checkedHtglHtbzZfjh.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleHtglHtbzZfjhSelectionChange(selection) {
      this.checkedHtglHtbzZfjh = selection.map(item => item.index)
    },
    /** 课题承担人员序号 */
    rowHtglHtbzCdryIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 课题承担人员添加按钮操作 */
    handleAddHtglHtbzCdry() {
      let obj = {};
      obj.name = "";
      obj.age = "";
      obj.zhiWu = "";
      obj.zhiChen = "";
      obj.zy = "";
      obj.bKtFg = "";
      obj.trZys = "";
      obj.gzDw = "";
      obj.kjryId = "";
      this.htglHtbzCdryList.push(obj);
    },
    /** 课题承担人员删除按钮操作 */
    handleDeleteHtglHtbzCdry() {
      if (this.checkedHtglHtbzCdry.length == 0) {
        this.$modal.msgError("请先选择要删除的课题承担人员数据");
      } else {
        const htglHtbzCdryList = this.htglHtbzCdryList;
        const checkedHtglHtbzCdry = this.checkedHtglHtbzCdry;
        this.htglHtbzCdryList = htglHtbzCdryList.filter(function (item) {
          return checkedHtglHtbzCdry.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleHtglHtbzCdrySelectionChange(selection) {
      this.checkedHtglHtbzCdry = selection.map(item => item.index)
    },
  }
};
</script>
